<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="90%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="文章标题" prop="title">
                        <el-input v-model="ruleForm.title" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="文章来源" prop="operator">
                        <el-input v-model="ruleForm.operator" class="w100p" clearable placeholder="请输入"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="显示/隐藏" prop="status">
                        <el-select v-model="ruleForm.status" class="w100p" clearable filterable placeholder="请选择">
                            <el-option label="显示" :value="true"></el-option>
                            <el-option label="隐藏" :value="false"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="关联栏目">
                        <el-select v-model="ruleForm.navigationId" class="w100p" clearable filterable placeholder="请选择">
                            <el-option v-for="item, index in navigationList" :key="index" :label="item.navigationName"
                                :value="item.navigationId"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="显示时间" prop="publishTime">
                        <el-date-picker class="w100p" v-model="ruleForm.publishTime" type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="文章" prop="content">
                <div class="quillContent" v-if="dialogVisible">
                    <Ueditor v-if="resetUEditor" :content="ruleForm.content || ''" @text_content="update"></Ueditor>
                </div>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import article from "@/api/article"
import Ueditor from '@/components/Ueditor';
export default {
    components: {  },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        userList: {
            type: Array,
            default: () => {
                return []
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        navigationList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        return {
            resetUEditor: true,
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                title: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                content: [
                    { required: true, message: '请编辑文字', trigger: 'change' }
                ],
                operator: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                publishTime: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.$set(this.ruleForm, 'status', true);
                this.addEditApi = article.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = article.edit;
                this.addEditName = '编辑';
                this.queryContent(this.data.articleId);
            }
            this.resetUEditor = false;
            this.$nextTick(() => {
                this.resetUEditor = true;
            })
        },
        async queryContent(articleId) {
            if (articleId) {
                let { data } = await article.queryContent(articleId);
                this.resetUEditor = false;
                this.$nextTick(() => {
                    this.resetUEditor = true;
                })
                this.$set(this.ruleForm, 'content', data.content);
            } else {
                this.$set(this.ruleForm, 'content', '');
            }
        },
        //获取文章
        update(e) {
            this.$set(this.ruleForm, 'content', e)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let success = res.code == 0
                        let msg = success ? '操作成功' : '操作失败'
                        this.$message[success ? 'success' : 'warning'](res.msg || msg)
                        success && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.quillContent {
    min-height: 300px;
}
</style>